<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;;
            padding: 0%;
        }
        .parent{
            width: 500px;
            height: 500px;
            background-color: aqua;
            margin: 50px auto;
            position: relative;
        }
        .box{
            width: 200px;
            height: 200px;
            background-color: orange;
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="box"></div>
    </div>
</body>
<script src="../js/tools.js"></script>
<script>
    let box=$(".box")

    box.onmousedown=function(e){
        let l=e.offsetX;
        let h=e.offsetY;
        document.onmousemove=function(e){
            let x=e.pageX-l-box.parentNode.offsetLeft;
            let y=e.pageY-h-box.parentNode.offsetTop;
            if(x<=0){
                x=0
            }else if(x>=box.parentNode.offsetWidth-box.offsetWidth){
                x=box.parentNode.offsetWidth-box.offsetWidth
            }

            if(y<=0){
                y=0
            }else if(y>=box.parentNode.offsetHeight-box.offsetHeight){
                y=box.parentNode.offsetHeight-box.offsetHeight
            }

            box.style.left=x+"px";
            box.style.top=y+"px";
        }
    }
    document.onmouseup=function(){
        document.onmousemove=null;
    }
</script>
</html>